// DAO/Impl/CourseDaoImpl.java
package com.hp.DAO.Impl;                         // 实现包

import com.hp.DAO.BaseDao;                       // 引入 BaseDao
import com.hp.DAO.CourseDao;                     // 引入接口 CourseDao
import com.hp.Model.Course;                      // 引入实体 Course
import com.hp.Util.RowMapper;                    // 行映射器
import com.hp.Util.SimpleJdbcTemplate;           // JDBC 模板

import java.sql.Timestamp;                // 时间戳
import java.util.Date;                    // Date
import java.util.List;                    // List

/** CourseDao 的 JDBC 实现 */
public class CourseDaoImpl extends BaseDao implements CourseDao { // 实现类
    public CourseDaoImpl(SimpleJdbcTemplate jdbc) {               // 构造器注入模板
        super(jdbc);                                              // 父类保存
    }

    // RowMapper：ResultSet -> Course
    private static final RowMapper<Course> M = rs -> {            // 映射器
        Course c = new Course();                                  // 创建实体
        c.setId(rs.getLong("id"));                                // id
        c.setTitle(rs.getString("title"));                        // 标题
        c.setBrief(rs.getString("brief"));                        // 简介
        c.setDifficulty(rs.getString("difficulty"));              // 难度
        c.setLangCode(rs.getString("lang_code"));                 // 语种
        c.setTeacherId(rs.getLong("teacher_id"));                 // 教师ID
        c.setStatus(rs.getString("status"));                      // 状态
        Timestamp ts = rs.getTimestamp("created_at");             // 创建时间
        c.setCreatedAt(ts == null ? null : new Date(ts.getTime())); // 转 Date
        return c;                                                 // 返回实体
    };

    @Override
    public Course findById(Long id) {                             // 按主键查课程
        String sql = "SELECT id,title,brief,difficulty,lang_code,teacher_id,status,created_at FROM course WHERE id=?"; // SQL
        return queryOne(sql, M, id);                              // 执行查询
    }

    @Override
    public List<Course> listByTeacher(Long teacherId, int offset, int limit) { // 按教师分页
        String sql = "SELECT id,title,brief,difficulty,lang_code,teacher_id,status,created_at " +
                "FROM course WHERE teacher_id=? ORDER BY id DESC LIMIT ? OFFSET ?"; // SQL
        return queryList(sql, M, teacherId, limit, offset);       // 执行查询
    }

    @Override
    public List<Course> searchByKeyword(String keyword, int offset, int limit) { // 关键字搜索
        String like = "%" + keyword + "%";                        // 模糊匹配
        String sql = "SELECT id,title,brief,difficulty,lang_code,teacher_id,status,created_at " +
                "FROM course WHERE title LIKE ? OR brief LIKE ? ORDER BY id DESC LIMIT ? OFFSET ?"; // SQL
        return queryList(sql, M, like, like, limit, offset);      // 执行查询
    }

    @Override
    public Long insert(Course c) {                                // 新增课程
        String sql = "INSERT INTO course (title,brief,difficulty,lang_code,teacher_id,status,created_at) VALUES (?,?,?,?,?,?,?)"; // SQL
        Date created = c.getCreatedAt() == null ? new Date() : c.getCreatedAt(); // 默认当前时间
        return execInsertReturnId(sql, c.getTitle(), c.getBrief(), c.getDifficulty(), c.getLangCode(),
                c.getTeacherId(), c.getStatus(), new Timestamp(created.getTime())); // 执行插入
    }

    @Override
    public int updateBasic(Course c) {                            // 更新基础字段
        String sql = "UPDATE course SET title=?, brief=?, difficulty=?, lang_code=?, status=? WHERE id=?"; // SQL
        return execUpdate(sql, c.getTitle(), c.getBrief(), c.getDifficulty(),
                c.getLangCode(), c.getStatus(), c.getId());       // 执行更新
    }

    @Override
    public int deleteById(Long id) {                              // 删除课程
        String sql = "DELETE FROM course WHERE id=?";             // SQL
        return execUpdate(sql, id);                               // 执行删除
    }
}
